/**
 * SoundCatalog.java
 * @author Daniel Liu
 * Create: 2006-12-22 ����11:03:26
 */
package org.lex.ftp.ui.prefs;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.AbstractTableModel;

import org.lex.app.prefs.ResourceMap;
import org.lex.awt.ComponentUtilities;
import org.lex.ftp.FTP;
import org.lex.ftp.util.prefs.Keys;
import org.lex.swing.prefs.CategoryAdapter;


public class SoundCategory extends CategoryAdapter {
	private JCheckBox enableSoundCheckBox = null;
	private JScrollPane scrollPane = null;
	private JTable table = null;
	private SoundTableModel tableModel = null;
	private JPanel controlPane = null;
	private JLabel fileLabel = null;
	private JTextField fileTextField = null;
	private JButton openButton = null;
	private JButton playButton = null;

	public SoundCategory() {
		super();
		initialize();
	}

	public String getTitle() {
		return "Sound";
	}

	public void loadConfiguration() {
		ResourceMap config = FTP.getContext().getResourceMap();
		String[] keys = { Keys.SOUND_CONNECT, Keys.SOUND_DISCONNECT, Keys.SOUND_COMPLETE, Keys.SOUND_ERROR };
		for (int i = 0; i < keys.length; i++)
			getTableModel().setSoundFile(config.getString(keys[i]), i);
		boolean enabled = config.getBoolean(Keys.SOUND_ENABLED);
		this.getEnableSoundCheckBox().setSelected(enabled);
	}

	public void saveConfiguration() {
		ResourceMap config = FTP.getContext().getResourceMap();
		String[] keys = { Keys.SOUND_CONNECT, Keys.SOUND_DISCONNECT, Keys.SOUND_COMPLETE, Keys.SOUND_ERROR };
		for (int i = 0; i < keys.length; i++)
			config.putProperty(keys[i], getTableModel().files[i]);
		boolean enabled = this.getEnableSoundCheckBox().isSelected();
		config.putProperty(Keys.SOUND_ENABLED, String.valueOf(enabled));
	}

	// =======================
	private void initialize() {
		BorderLayout borderLayout = new BorderLayout();
		borderLayout.setVgap(5);
		this.setLayout(borderLayout);
		this.setBorder(BorderFactory.createTitledBorder(null, "Sound", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font(
				"Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
		this.add(getNorthPane(), BorderLayout.NORTH);
		this.add(getScrollPane(), BorderLayout.CENTER);
		this.add(getControlPane(), BorderLayout.SOUTH);
		this.add(Box.createHorizontalStrut(5), BorderLayout.WEST);
		this.add(Box.createHorizontalStrut(6), BorderLayout.EAST);
	}

	private JCheckBox getEnableSoundCheckBox() {
		if (enableSoundCheckBox == null) {
			enableSoundCheckBox = new JCheckBox();
			enableSoundCheckBox.setText("Enable Sound Events");
			enableSoundCheckBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					boolean selected = enableSoundCheckBox.isSelected();
					ComponentUtilities.setEnabled(selected, getRestoreButton(), getTable(), fileLabel, getFileTextField(), getOpenButton(),
							getPlayButton());
				}
			});
		}
		return enableSoundCheckBox;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.setModel(this.getTableModel());
		}
		return table;
	}

	private SoundTableModel getTableModel() {
		if (null == this.tableModel) {
			this.tableModel = new SoundTableModel();
		}
		return this.tableModel;
	}

	private JPanel getControlPane() {
		if (controlPane == null) {
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.insets = new Insets(1, 6, 5, 0);
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 3;
			gridBagConstraints2.insets = new Insets(1, 6, 5, 5);
			gridBagConstraints2.gridy = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 2;
			gridBagConstraints1.insets = new Insets(1, 6, 5, 0);
			gridBagConstraints1.gridy = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.insets = new Insets(1, 6, 5, 0);
			gridBagConstraints.gridx = 1;
			fileLabel = new JLabel();
			fileLabel.setText("File");
			controlPane = new JPanel();
			controlPane.setLayout(new GridBagLayout());
			controlPane.add(fileLabel, gridBagConstraints3);
			controlPane.add(getFileTextField(), gridBagConstraints);
			controlPane.add(getOpenButton(), gridBagConstraints1);
			controlPane.add(getPlayButton(), gridBagConstraints2);
		}
		return controlPane;
	}

	private JTextField getFileTextField() {
		if (fileTextField == null) {
			fileTextField = new JTextField();
		}
		return fileTextField;
	}

	private JButton getOpenButton() {
		if (openButton == null) {
			openButton = new JButton();
			openButton.setText("Open");
			openButton.addActionListener(new ActionListener() {
				private File currentDir = new File("./ftp/sound/");

				public void actionPerformed(ActionEvent e) {
					if (false == currentDir.exists())
						currentDir = new File(".");
					JFileChooser fc = SoundFileChooser.newFileChooser(currentDir);
					int selection = fc.showOpenDialog(ComponentUtilities.getAncestor(Dialog.class, SoundCategory.this));
					if (JFileChooser.APPROVE_OPTION == selection) {
						File file = fc.getSelectedFile();
						if (null == file)
							return;
						getFileTextField().setText(file.getAbsolutePath());
						currentDir = file.getParentFile();
					}
				}
			});
		}
		return openButton;
	}

	private JButton getPlayButton() {
		if (playButton == null) {
			playButton = new JButton();
			playButton.setText("Play");
		}
		return playButton;
	}

	private JPanel getNorthPane() {
		if (northPane == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.insets = new Insets(0, 0, 1, 6);
			gridBagConstraints5.gridy = 0;
			gridBagConstraints5.ipady = 0;
			gridBagConstraints5.anchor = GridBagConstraints.EAST;
			gridBagConstraints5.fill = GridBagConstraints.NONE;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.insets = new Insets(0, 3, 1, 0);
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.gridx = 0;
			northPane = new JPanel();
			northPane.setLayout(new GridBagLayout());
			northPane.add(getEnableSoundCheckBox(), gridBagConstraints4);
			northPane.add(getRestoreButton(), gridBagConstraints5);
		}
		return northPane;
	}

	private JButton getRestoreButton() {
		if (restoreButton == null) {
			restoreButton = new JButton();
			restoreButton.setText("Restore Defaults");
		}
		return restoreButton;
	}

	private static final String[] columnNames = { "Event", "Sound File" };
	private JPanel northPane = null;
	private JButton restoreButton = null;

	class SoundTableModel extends AbstractTableModel {
		private String[] events = null;
		private String files[] = null;

		public SoundTableModel() {
			super();
			this.events = new String[] { "Connect", "Disconnect", "Queue Complete", "Transfer Error" };
			this.files = new String[this.events.length];
		}

		public String getColumnName(int column) {
			return columnNames[column];
		}

		public int getColumnCount() {
			return columnNames.length;
		}

		public int getRowCount() {
			return this.files.length;
		}

		public Object getValueAt(int row, int column) {
			if (0 == column)
				return this.events[row];
			else
				return this.files[row];
		}

		public void setSoundFile(String file, int row) {
			if (null == file)
				file = "";
			this.files[row] = file;
			this.fireTableDataChanged();
		}
	}
}
